package com.yanqu.road.dao.impl.drugstore;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.DrugHardDispenseRankData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/4/18 18:20
 */
public class DrugHardDispenseRankDataDaoImpl extends TempDataObjectDao<DrugHardDispenseRankData> {
    public static String getDrugHardDispenseRankDataSql = "select * from `t_u_drug_store_hardcase_rank`";
    public Map<Integer, DrugHardDispenseRankData> getDrugHardDispenseRankDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DrugHardDispenseRankData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDrugHardDispenseRankDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DrugHardDispenseRankData rankData = parse(rs);
                map.put(rankData.getCaseId(), rankData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDrugHardDispenseRankDataSql = "update `t_u_drug_store_hardcase_rank` set time=? where caseId=? and userId=?";
    @Override
    protected boolean updateData(DrugHardDispenseRankData drugHardDispenseRankData) {
        drugHardDispenseRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, drugHardDispenseRankData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardDispenseRankData.getCaseId()));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardDispenseRankData.getUserId()));
        if (getDb().executeNoneQuery(updateDrugHardDispenseRankDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDrugHardDispenseRankDataSql = "insert into `t_u_drug_store_hardcase_rank`(caseId,userId,time) values (?,?,?)";
    @Override
    protected boolean addData(DrugHardDispenseRankData drugHardDispenseRankData) {
        drugHardDispenseRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, drugHardDispenseRankData.getCaseId()));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardDispenseRankData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardDispenseRankData.getTime()));
        if (getDb().executeNoneQuery(insertDrugHardDispenseRankDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DrugHardDispenseRankData parseData(ResultSet rs) throws SQLException {
        DrugHardDispenseRankData drugHardDispenseRankData = new DrugHardDispenseRankData();
        drugHardDispenseRankData.setCaseId(rs.getInt("caseId"));
        drugHardDispenseRankData.setUserId(rs.getLong("userId"));
        drugHardDispenseRankData.setTime(rs.getLong("time"));

        return drugHardDispenseRankData;
    }
}
